unit UDM;

interface

uses
  SysUtils, Classes, DB, ADODB, URegistroCamargo;

type
  TDMPrincipal = class(TDataModule)
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;

  private
    { Private declarations }
    FStringConexao: String;
    FADOQry: TADOQuery;
    FADOQryPrincipal: TADOQuery;
    FADOConnection1: TADOConnection;
    CfgRegDBDM: TRegistroCamargo;
    FAmbiente: String;

    function GetStringConexao: String;
    procedure SetStringConexao(const Value: String);

  public
    { Public declarations }
    constructor Create;overload;
    constructor Create(const iTipoDB: Integer);overload;
    function TestaConexaoDB: Boolean;
    function ValidaUsuario: Boolean;
    function GetDMADOQuery: TADOQuery;
    procedure SetDMADOQuery(const Value: TADOQuery);

    function GetAmbiente: String;
    procedure SetAmbiente(const Value: Integer);
  end;

var
  DMPrincipal: TDMPrincipal;

implementation

uses Dialogs;

{$R *.dfm}

{ TDMPrincipal }

constructor TDMPrincipal.Create;
  var
    sPwd, sUser, sDB, sServidor: String;
    DateCur: TDateTime;
begin
    FADOConnection1 := TADOConnection.Create(nil);
    CfgRegDBDM := TRegistroCamargo.Create;
    if(CfgRegDBDM.ReadRegistryRec(Self.GetAmbiente).DefaultTypeDBi = 0)then
    begin
      sServidor := CfgRegDBDM.ReadRegistryRec(Self.GetAmbiente).DefaultTypeServers;
      sUser := CfgRegDBDM.ReadRegistryRec(Self.GetAmbiente).UserDBs;
      sPwd := CfgRegDBDM.ReadRegistryRec(Self.GetAmbiente).PasswordDBs;
      sDB := CfgRegDBDM.ReadRegistryRec(Self.GetAmbiente).DefaultTypeDBs;
      DateCur := CfgRegDBDM.ReadRegistryRec(Self.GetAmbiente).DateCorrente;
      
      FADOConnection1.ConnectionString := 'Provider=SQLOLEDB.1;Password=' + sPwd + ';Persist Security Info=True;User ID=' + sUser + ';Initial Catalog=' + sDB + ';Data Source=' + sServidor + ';Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=CAMARGO-VIRTUAL;Use Encryption for Data=False;Tag with column collation when possible=False';
    end;
    FADOQryPrincipal := TADOQuery.Create(nil);
    FADOQryPrincipal.Connection := FADOConnection1;

    FStringConexao := ADOConnection1.ConnectionString;
  //end
  //else
  //begin
    //ShowMessage('Ocorreu erro no registro do Banco-Zero');
  //end;

end;

constructor TDMPrincipal.Create(const iTipoDB: Integer);
  var
    sPwd, sUser, sDB, sServidor, sAmbiente: String;
    DateCur: TDateTime;
begin
  case iTipoDB of
    0: sAmbiente := 'Producao';
    1: sAmbiente := 'Teste';
    2: sAmbiente := 'Desenvolvimento';
  end; // fim case

  Self.SetAmbiente(iTipoDB);

  CfgRegDBDM := TRegistroCamargo.Create;

  sServidor := CfgRegDBDM.ReadRegistryRec(sAmbiente).DefaultTypeDBs;
  sUser := CfgRegDBDM.ReadRegistryRec(sAmbiente).UserDBs;
  sPwd := CfgRegDBDM.ReadRegistryRec(sAmbiente).PasswordDBs;
  DateCur := CfgRegDBDM.ReadRegistryRec(sAmbiente).DateCorrente;

  FADOConnection1 := TADOConnection.Create(nil);
  FADOConnection1.ConnectionString := 'Provider=SQLOLEDB.1;Password=' + sPwd + ';Persist Security Info=True;User ID=' + sUser + ';Initial Catalog=' + sDB + ';Data Source=' + sServidor + ';Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=CAMARGO-VIRTUAL;Use Encryption for Data=False;Tag with column collation when possible=False';

  FADOQryPrincipal := TADOQuery.Create(nil);
  FADOQryPrincipal.Connection := FADOConnection1;

  FStringConexao := ADOConnection1.ConnectionString;
end;

function TDMPrincipal.GetAmbiente: String;
begin
  Result := FAmbiente;
end;

function TDMPrincipal.GetDMADOQuery: TADOQuery;
begin
  Result := FADOQry;
end;

function TDMPrincipal.GetStringConexao: String;
begin
  Result := FStringConexao;
end;

procedure TDMPrincipal.SetDMADOQuery(const Value: TADOQuery);
begin
  FADOQry := Value;
end;


procedure TDMPrincipal.SetAmbiente(const Value: Integer);
begin
  if(Value >= 0)then
  begin
    case Value of
      0: FAmbiente := 'Producao';
      1: FAmbiente := 'Teste';
      2: FAmbiente := 'Desenvolvimento';
    end; // fim case
  end
  else
  begin
    FAmbiente:= 'No';
  end;
end;

procedure TDMPrincipal.SetStringConexao(const Value: String);
begin
  FStringConexao := Value;
end;

function TDMPrincipal.TestaConexaoDB: Boolean;
  var
    ADOQryTestConn: TADOQuery;
begin
  try
    try
      ADOQryTestConn := TADOQuery.Create(Self);
      ADOQryTestConn.Connection := ADOConnection1;
      ADOQryTestConn.Close;
      ADOQryTestConn.SQL.Clear;
      ADOQryTestConn.SQL.Text := 'SELECT id_usuario FROM usuarios WHERE id_usuario <= 5';
      ADOQryTestConn.Open;

      if(ADOQryTestConn.RecordCount > 0)then
      begin
        Result := True;
      end
      else
      begin
        Result := False;
      end;
    except
      On Erro: Exception do
      begin
        ShowMessage('ERRO : [TDMPrincipal].[TestConnection] ' + #13 + #13 + 'Msg.: ' + Erro.Message);
      end;
    end; // fim try except
  finally
    ADOQryTestConn.Free;
  end; // fim try finally
end;

function TDMPrincipal.ValidaUsuario: Boolean;
  var
    ADOQryMain: TADOQuery;
begin
  try
    if(Not ADOConnection1.Connected)then
    begin
      ADOConnection1.Connected := True;
    end;

    try
      ADOQryMain := TADOQuery.Create(Self);
      ADOQryMain.Connection := ADOConnection1;
      ADOQryMain.Close;
      ADOQryMain.SQL.Clear;

      ADOQryMain.SQL.Text := 'SELECT UPPER(usu_login), usu_senha FROM usuarios';
      //ADOQryMain.Parameters.ParamByName('USERX').Value := Self.GetUsuario;
      //ADOQryMain.Parameters.ParamByName('PWDX').Value := Self.GetSenha;
      ADOQryMain.Open;

      if(ADOQryMain.RecordCount > 0)then
      begin
        Self.SetDMADOQuery(ADOQryMain);
        {*
        while(not ADOQryMain.Eof)do
        begin
          Self.SetApelido(ADOQryMain.FieldByName('usu_login').AsString);
          ADOQryMain.Next;
        end; // fim while
        *}

        Result := True;
      end
      else
      begin
        Result := False;
      end;
    except
      On Erro: Exception do
      begin
        ShowMessage('ERRO : [TDMPrincipal].[TestConnection] ' + #13 + #13 + 'Msg.: ' + Erro.Message);
      end;
    end; // fim try except
  finally
    ADOQryMain.Free;
  end; // fim try finally
end;



end.
